//
//  XMMyMeetingTopTabView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/23.
//

#import "XMMyMeetingTopTabView.h"

@interface XMMyMeetingTopTabView ()

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSMutableArray *itemBtnArr;

@end

@implementation XMMyMeetingTopTabView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.lineView];
    NSArray *titleArr = @[@"在卖", @"草稿", @"已下架"];
    CGFloat itemWidth = (self.width - KScaleWidth(97)*2)/3;
    for (int i = 0; i < 3; i ++) {
        UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(15.0) frame:CGRectMake(KScaleWidth(97.0) + itemWidth*i, KScaleWidth(10.0), itemWidth, KScaleWidth(34.0)) title:titleArr[i] selectTitle:titleArr[i] titleColor:kColorHEX(0xBDBDBD) selectColor:kColorHEX(0x333333)];
        [itemBtn addTarget:self action:@selector(itemSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        itemBtn.tag = 8000 + i;
        [self addSubview:itemBtn];
        [self.itemBtnArr addObject:itemBtn];
        if (i == 0) {
            _lineView.x = itemBtn.x + (itemWidth - _lineView.width)/2;
            itemBtn.titleLabel.font = KBoldFontSize(14.0);
            itemBtn.selected = YES;
        }
    }
}

- (void)itemSelectAction:(UIButton *)button {
    for (UIButton *itemBtn in self.itemBtnArr) {
        itemBtn.selected = NO;
        itemBtn.titleLabel.font = KFontSize(14.0);
    }
    button.selected = YES;
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.lineView.x = button.x + (button.width - weakSelf.lineView.width)/2;
        button.titleLabel.font = KBoldFontSize(14.0);
    }];
    self.itemSelectBlock ? self.itemSelectBlock(button.tag - 8000) : nil;
}

#pragma mark - Init
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(10.0) + KScaleWidth(32.0), KScaleWidth(25.0), 2)];
        [_lineView setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [_lineView setCornerRadius:1];
    }
    return _lineView;
}

- (NSMutableArray *)itemBtnArr {
    if (!_itemBtnArr) {
        _itemBtnArr = [NSMutableArray array];
    }
    return _itemBtnArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
